package org.jackysoft.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.util.Assert;

public final class IOUtils extends org.apache.commons.io.IOUtils {

	static final Log log = LogFactory.getLog(IOUtils.class);
	public static final String[] NW_EXCLUSIVE = {"^\\.","^_","md$","\\.gyp$","\\.log$","test"
		,"README","Makefile","\\.as$","LICENSE","\\.csv$","\\.c$","\\.h$"
		,"\\.less$"};
	public static InputStream toInputStream(byte[] content) {
		ByteArrayInputStream bis = new ByteArrayInputStream(content);
		return bis;
	}

	public static byte[] toByteArray(File source) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		InputStream ins;
		try {
			ins = new FileInputStream(source);
			byte[] buffer = new byte[1024];
			int d = -1;
			while ((d = ins.read(buffer)) != -1) {
				bos.write(buffer, 0, d);
			}
			ins.close();
		} catch (FileNotFoundException e) {
			log.error(e);
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e);
			e.printStackTrace();
		}

		return bos.toByteArray();
	}

	public static void saveFile(File source, File target) {
		try {
			BufferedOutputStream bw = new BufferedOutputStream(
					new FileOutputStream(target));
			BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(source));

			int data = -1;
			byte[] buf = new byte[2048];
			while ((data = bis.read(buf)) != -1) {
				bw.write(buf, 0, data);
			}
			bw.close();
			bis.close();
		} catch (FileNotFoundException e) {
			log.error(e);
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e);
			e.printStackTrace();
		}

	}

	public static void saveFile(byte[] source, File target) {
		try {
			BufferedOutputStream bw = new BufferedOutputStream(
					new FileOutputStream(target));
			BufferedInputStream bis = new BufferedInputStream(
					new ByteArrayInputStream(source));

			int data = -1;
			byte[] buf = new byte[2048];
			while ((data = bis.read(buf)) != -1) {
				bw.write(buf, 0, data);

			}
			bw.close();
			bis.close();
		} catch (FileNotFoundException e) {
			log.error(e);
			e.printStackTrace();
		} catch (IOException e) {
			log.error(e);
			e.printStackTrace();
		}

	}

	/**
	 * 把一组文件压缩成一个字节数组
	 * 
	 * @param 文件集合
	 * @return 文件字节数组
	 * */
	public static byte[] zipFiles(Collection<File> files) {
		byte[] bs = new byte[0];
		String baseDir = System.getProperty("java.io.tmpdir") + File.separator;
		UUID uid = UUID.randomUUID();
		String fileName = baseDir + uid.toString() + ".zip";
		try {
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
					fileName));
			out.setEncoding("UTF-8");
			for (File f : files) {
				String name = f.getName();
				if (f instanceof CustomFile) {
					CustomFile cf = (CustomFile) f;
					name = cf.getTitleName();

				}
				out.putNextEntry(new org.apache.tools.zip.ZipEntry(name));
				FileInputStream in = new FileInputStream(f);
				int b;
				while ((b = in.read()) != -1) {
					out.write(b);
				}
				in.close();
			}
			out.close();
			bs = org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(
					fileName));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bs;
	}

	public static void makeDirectory(File file) {
		Assert.notNull(file);

		if (file.exists())
			return;
		if (!file.mkdirs()) {
			log.error("File created failed! ");
		}
	}

	public static void makeDirectories(File... files) {
		for (File path : files) {
			makeDirectory(path);
		}

	}

	public static final void checkFile(int from, File dir,
			List<String> shortfiles, String... exclsive) {
		if (shortfiles == null)
			return;
		
		if (dir.isDirectory()) {
			File[] files = dir.listFiles();
			SFILEFOR: for (File f : files) {
				String fn = f.getName();
				if (exclsive != null) {
					for (String elv : exclsive) {
						Pattern ptn = Pattern.compile(elv);
						if (ptn.matcher(fn).find())
							continue SFILEFOR;
					}
				}

				if (f.isFile()) {
					shortfiles.add(clearPrefix(from, f.getAbsolutePath()));
				} else {
					checkFile(from,f,shortfiles, exclsive);
				}
			}
		} else {
			shortfiles.add(clearPrefix(from, dir.getAbsolutePath()));
		}

	}

	public static final String clearPrefix(int from,String path) {
		
		String[] ps  = path.split("\\\\");
        String[] nps = Arrays.copyOfRange(ps, from, ps.length);
        String npath= "";
        boolean first=true;
        for(String s :nps){
     	   npath+=(first?"":File.separator)+s;
           first=false;
        }
        
        return npath;
	}
	
	public static final int fromPos(String dirpath){
		File base=  new File(dirpath);
		String basePath = base.getAbsolutePath();
		String[] bps = basePath.split("\\\\");
		int from  = bps.length;
		return from;
	}
	
	

}
